#include "WProgram.h"
#include "WConstants.h"
#include "BlueToothM5.h"

#include <inttypes.h>
#include <avr/interrupt.h>

#include <string.h>

String readString2 = "";

int disponivel = 0;

BTM5::BTM5()
{
  init();
}

void BTM5::init()
{
  begin(38400);
  
  disponivel = 1;  
}

void BTM5::begin(long speed)
{
  Serial2.begin(speed);
}

String BTM5::getRole()
{
  return VerificaRetorno("AT+ROLE=\r\n");  
}

void BTM5::setRoleMaster()
{
  Serial2.write("AT+ROLE=0\r\n");
  delay(100);
}

void BTM5::setRoleSlave()
{
  Serial2.write("AT+ROLE=2\r\n");
  delay(100);
}

String BTM5::getBind() 
{
  return VerificaRetorno("AT+BIND\r\n");  
}

void BTM5::setBind(const char *s)
{
  Serial2.write("AT+BIND=");
  Serial2.write(s);
  Serial2.write("\r\n");
  delay(10);
}

String BTM5::getPassword() 
{
  return VerificaRetorno("AT+PSWD\r\n");  
}

void BTM5::setPassword(const char *s)
{
  Serial2.write("AT+PSWD=");
  Serial2.write(s);
  Serial2.write("\r\n");
  delay(10);
}

String BTM5::getLinkMode() 
{
  return VerificaRetorno("AT+CMODE\r\n");  
}

void BTM5::setLinkModeSpecify()
{
  Serial2.write("AT+CMODE=0\r\n");
  delay(10);
}

void BTM5::setLinkModeAny()
{
  Serial2.write("AT+CMODE=1\r\n");
  delay(10);
}

void BTM5::setLinkModeSlaveLoopBack()
{
  Serial2.write("AT+CMODE=2\r\n");
  delay(10);
}

String BTM5::getDeviceName() 
{
  return VerificaRetorno("AT+NAME\r\n");  
}

String BTM5::getRemoteDeviceName(const char *s) 
{
  pinMode(12, OUTPUT);
  digitalWrite(12, HIGH);
  delay(200);

  String readString;    

  Serial2.write("AT+RNAME?");
  Serial2.write(s);
  Serial2.write("\r\n");
  
  delay(200);

  while (Serial2.available()) 
  {
    delay(10);

    if (Serial2.available() > 0) 
    {
      byte c = Serial2.read();
      readString += c;
    }
  } 

  delay(10);
  
  digitalWrite(12, LOW);
    
  return readString;
}


//Corrigir. Falta parametro de nome
void BTM5::setDeviceName(const char *s) 
{
  Serial2.write("AT+NAME=");
  Serial2.write(s);
  Serial2.write("\r\n");
  delay(10);
}

String BTM5::getFirmware()
{
  return VerificaRetorno("AT+VERSION\r\n");  
}

String BTM5::getAddress()
{
  return VerificaRetorno("AT+ADDR\r\n");
}

void BTM5::reset()
{
  Serial2.write("AT+RESET\r\n");
  delay(10);
}

void BTM5::restoredefault()
{
  Serial2.write("AT+ORGL\r\n");
  delay(10);
}

void BTM5::write(const char *s)
{
 Serial2.write(s);
}

int BTM5::available()
{
  int disponivelaux = disponivel;

  disponivel = 1;
  
  return disponivelaux;
}

String BTM5::read()
{
  String readString3;    
  

  while (Serial2.available()) 
  {
    disponivel = 0;
    delay(10);

    if (Serial2.available() > 0) 
    {
      byte c = Serial2.read();
      readString3 += c;
    }
  } 

  delay(10);
  
  return readString3;
}

void BTM5::EscreveComando(const char *comando)
{
  pinMode(12, OUTPUT);
  digitalWrite(12, HIGH);
  delay(200);

  Serial2.write(comando);
  
  delay(2000);
  
  digitalWrite(12, LOW);
}


String BTM5::VerificaRetorno(const char *comando)
{
  pinMode(12, OUTPUT);
  digitalWrite(12, HIGH);
  delay(200);

  String readString;    

  Serial2.write(comando);
  
  delay(100);

  while (Serial2.available()) 
  {
    delay(10);

    if (Serial2.available() > 0) 
    {
      byte c = Serial2.read();
      readString += c;
    }
  } 

  delay(10);
  
  digitalWrite(12, LOW);
    
  return readString;
}


